package com.rongshu.view.userInfo.activity;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.rongshu.R;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.discovery.bean.ArticleDetaisRequest;
import com.rongshu.view.home.activity.SingleArticleDetailsActivity;
import com.rongshu.view.home.entity.UserInfo;
import com.rongshu.view.userInfo.adapter.CollectedSerializeAdapter;
import com.rongshu.view.userInfo.entity.CollectedArticle;
import com.rongshu.view.userInfo.iview.CollectedArticleView;
import com.rongshu.view.userInfo.presenter.CollectedArticlePresenter;
import com.rongshu.widget.BanyanDialog;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

public class CollectedSerializeAcitivity extends BaseActivity<CollectedArticlePresenter> implements CollectedArticleView, CollectedSerializeAdapter.OnItemFunctionClickListener {
    @BindView(R.id.txt_top_title)
    TextView txt_top_title;
    @BindView(R.id.collected_works_recyclerview)
    RecyclerView collected_works_recyclerview;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.layout_empty)
    RelativeLayout layout_empty;

    //manager
    private LinearLayoutManager manager = null;
    //adapter
    private CollectedSerializeAdapter mCollectedSerializeAdapter = null;
    //dialog
    private BanyanDialog dialog = null;
    //intent
    private Intent intent = null;
    //userInfo
    private UserInfo.ResponseBean userInfo = null;
    //文集id
    private int serialize = 0;
    //文集名称
    private String articleName = "";
    //map
    private Map<String, Object> map = null;
    private int pageIndex = 1;
    private int pageSize = 10;
    //refresh,load
    private boolean isrefresh = true;
    //筛选类型 type
    private int type = 0;

    @Override
    protected int initLayout() {
        return R.layout.activity_collected_serialize_view;
    }

    @Override
    protected void initView() {
        //set presenter
        setPresenter(new CollectedArticlePresenter(this), this);
        //intent
        intent = getIntent();
        userInfo = intent.getParcelableExtra(Constant.INTENT_DATA);
        serialize = intent.getIntExtra(Constant.INTENT_MESSAGE, 0);
        articleName = intent.getStringExtra(Constant.INTENT_TITLE);
        //title
        txt_top_title.setText(articleName);
        //layoutmanager
        manager = new LinearLayoutManager(this);
        //adapter
        mCollectedSerializeAdapter = new CollectedSerializeAdapter(this);
        mCollectedSerializeAdapter.setOnItemFunctionClickListener(this);
        //recyclerview
        collected_works_recyclerview.setLayoutManager(manager);
        collected_works_recyclerview.setAdapter(mCollectedSerializeAdapter);
        //触发自动刷新
        refreshLayout.autoRefresh();
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshlayout) {
                isrefresh = true;
                pageIndex = 1;
                refreshlayout.resetNoMoreData();
                if (serialize != 0) {
                    getMyArticle(serialize);
                }
                refreshlayout.finishRefresh(2000, false);
            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                isrefresh = false;
                pageIndex++;
                if (serialize != 0) {
                    getMyArticle(serialize);
                }
                refreshlayout.finishLoadmore(2000, false);
            }
        });
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void updateViews(boolean isRefresh) {

    }

    /**
     * APP获取文集下的文章列表
     */
    private void getMyArticle(int serialize_id) {
        //user_id
        int user_id = UserInfoUtils.getUserId();
        map = new HashMap<>();
        map.put("user_id", user_id);
        map.put("serialize_id", serialize_id);
        map.put("type", type);//(0全部 1已发布 2审核中 3草稿)
        map.put("page", pageIndex);
        map.put("pagesize", pageSize);
        presenter.getMyArticle(user_id, serialize_id, type, pageIndex, pageSize, Common.encryptionSign(map));
        map.clear();
    }

    /**
     * APP删除文章
     */
    private void getDeleteContribute(int contribute_id) {
        //user_id
        int user_id = UserInfoUtils.getUserId();
        map = new HashMap<>();
        map.put("user_id", user_id);
        map.put("contribute_id", contribute_id);
        presenter.getDeleteContribute(user_id, contribute_id, Common.encryptionSign(map));
        map.clear();
    }

    /**
     * 提示
     */
    public void showCollectedDialog(final int contribute_id, int is_check) {
        //dialog
        BanyanDialog.Builder builder = new BanyanDialog.Builder(this);
        builder.setTitle(is_check == 1 || is_check == 2 ? "已发表状态无法删除" : "删除文章");
        builder.setContent(is_check == 1 || is_check == 2 ? "如需删除\n请联系榕书编辑" : "文章将移至回收站 可保存60天");
        builder.setLeft(is_check == 1 || is_check == 2 ? "联系方式：RS@rongshu.com" : getString(R.string.cancle));
        if (is_check == 3) {
            builder.setRight(getString(R.string.alert_confirm));
        }
        builder.setDialogOnClick(new BanyanDialog.Builder.onClickListener() {
            @Override
            public void leftClickListener() {
                dialog.dismiss();
            }

            @Override
            public void rightClickListener() {
                dialog.dismiss();
                getDeleteContribute(contribute_id);
            }
        });
        dialog = builder.create();
        dialog.show();
    }

    @Override
    public void onClick(int contribute_id) {
        intent = new Intent(this, SingleArticleDetailsActivity.class);
        ArticleDetaisRequest request = new ArticleDetaisRequest();
        request.setContribute_id(contribute_id);
        request.setType(1);
        request.setTopic_id(0);
        intent.putExtra(Constant.ARTICLE_DETAILS_REQUEST, request);
        startActivity(intent);
    }

    @Override
    public void onDeleteClick(int contribute_id, int is_check) {
        showCollectedDialog(contribute_id, is_check);
    }

    @OnClick({R.id.layout_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.layout_back:
                finish();
                break;
        }
    }

    /**
     * APP获取文集下的文章列表回调
     */
    @Override
    public void onCollectedArticleList(CollectedArticle collectedArticle) {
        if (collectedArticle == null) {
            return;
        }
        List<CollectedArticle.ResponseBean> authorDetailBeans = collectedArticle.getResponse();
        if (mCollectedSerializeAdapter != null) {
            //设置文集数
            mCollectedSerializeAdapter.setCollectedArticleSum(collectedArticle.getTotal());
            if (isrefresh) {
                refreshLayout.finishRefresh();
                if (authorDetailBeans == null || authorDetailBeans.isEmpty()) {
                    refreshLayout.setVisibility(View.VISIBLE);
                    layout_empty.setVisibility(View.VISIBLE);
                    mCollectedSerializeAdapter.setCollectedArticles(authorDetailBeans);
                    mCollectedSerializeAdapter.notifyDataSetChanged();
                } else {
                    refreshLayout.setVisibility(View.VISIBLE);
                    layout_empty.setVisibility(View.GONE);
                    mCollectedSerializeAdapter.setCollectedArticles(authorDetailBeans);
                    mCollectedSerializeAdapter.notifyDataSetChanged();
                }
            } else {
                if (authorDetailBeans == null || authorDetailBeans.isEmpty()) {
                    refreshLayout.finishLoadmoreWithNoMoreData();
                } else {
                    refreshLayout.finishLoadmore();
                    mCollectedSerializeAdapter.addCollectedArticles(authorDetailBeans);
                    mCollectedSerializeAdapter.notifyDataSetChanged();
                }
            }
        }
    }

    /**
     * APP删除文章
     *
     * @param bean
     */
    @Override
    public void onDeleteContribute(INetBean bean) {
        isrefresh = true;
        pageIndex = 1;
        if (serialize != 0) {
            getMyArticle(serialize);
        }
    }

    @Override
    public void onMoveContribute(INetBean bean) {

    }
}
